package top.suven.base.core.mq.rocketmq;

import top.suven.base.util.constants.GlobalConstants;
import org.apache.rocketmq.client.log.ClientLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @ClassName:
 * @Description:
 * @Author lixiangling
 * @Date 2018/5/22 18:15
 * @Copyright: (c) 2018 gc by https://www.gc.com.cn/
 * @Version : 1.0.0
 * --------------------------------------------------------
 * modifyer    modifyTime                 comment
 *
 * --------------------------------------------------------
 */
@Configuration
@ConfigurationProperties(prefix = GlobalConstants.ROCKT_MQ_CONFIG_PREFIX)
@EnableConfigurationProperties({RocketMQConfig.class})
@ConditionalOnProperty(name = GlobalConstants.ROCKT_MQ_CONFIG_ENABLED,  matchIfMissing = false)
public class RocketMQAutoConfig {

    private Logger logger = LoggerFactory.getLogger(RocketMQAutoConfig.class);

    private RocketMQListenerFactory rocketMQListenerFactory;
    private RocketMQProducerFactory rocketMQProducerFactory;


    @Bean
    public RocketMQAutoConfig rocketMQConfig() {
        RocketMQAutoConfig config =  new RocketMQAutoConfig();
//        config.getClient().systemProperty();
        logger.info("RocketMQAutoConfig init rocketMQConfiguration =[{}]", config.toString());
        return config;
    }


    @Bean
    public RocketMQListenerFactory rocketMQListenerFactory() {
        rocketMQListenerFactory = new RocketMQListenerFactory();
        return rocketMQListenerFactory;
    }

    @Bean
    public RocketMQConsumerFactory rocketMQConsumerFactory() {
        return new RocketMQConsumerFactory(rocketMQConfig(),rocketMQListenerFactory);
    }

    @Bean
    public RocketMQProducerFactory rocketMQProducerFactory() {
        rocketMQProducerFactory =  new RocketMQProducerFactory(rocketMQConfig());
        return rocketMQProducerFactory;
    }


    public RocketMQListenerFactory getRocketMQListenerFactory(){
        return rocketMQListenerFactory;
    }

    public RocketMQProducerFactory getRocketMQProducerFactory(){
        return rocketMQProducerFactory;
    }


    private String nameSrvAdds;
    private String instanceName;
    private int maxMessageSize;
    private int sendMsgTimeout;
    private int retryTimesWhenSendFailed;
    private int consumeThreadMin;
    private int consumeThreadMax;

    private ClientLog client = new ClientLog().systemProperty();


    public String getNameSrvAdds() {
        return nameSrvAdds;
    }

    public void setNameSrvAdds(String nameSrvAdds) {
        this.nameSrvAdds = nameSrvAdds;
    }

    public String getInstanceName() {
        return instanceName;
    }

    public void setInstanceName(String instanceName) {
        this.instanceName = instanceName;
    }

    public int getMaxMessageSize() {
        return maxMessageSize;
    }

    public void setMaxMessageSize(int maxMessageSize) {
        this.maxMessageSize = maxMessageSize;
    }

    public int getSendMsgTimeout() {
        return sendMsgTimeout;
    }

    public void setSendMsgTimeout(int sendMsgTimeout) {
        this.sendMsgTimeout = sendMsgTimeout;
    }

    public int getConsumeThreadMin() {
        return consumeThreadMin;
    }

    public void setConsumeThreadMin(int consumeThreadMin) {
        this.consumeThreadMin = consumeThreadMin;
    }

    public int getConsumeThreadMax() {
        return consumeThreadMax;
    }

    public void setConsumeThreadMax(int consumeThreadMax) {
        this.consumeThreadMax = consumeThreadMax;
    }

    public int getRetryTimesWhenSendFailed() {
        return retryTimesWhenSendFailed;
    }

    public void setRetryTimesWhenSendFailed(int retryTimesWhenSendFailed) {
        this.retryTimesWhenSendFailed = retryTimesWhenSendFailed;
    }

    public ClientLog getClient() {
        return client;
    }

    public void setClient(ClientLog client) {
        this.client = client;
    }

    @Override
    public String toString() {
        return "RocketMQConfig{" +
                "nameSrvAdds='" + nameSrvAdds + '\'' +
                ", instanceName='" + instanceName + '\'' +
                ", maxMessageSize=" + maxMessageSize +
                ", sendMsgTimeout=" + sendMsgTimeout +
                ", retryTimesWhenSendFailed=" + retryTimesWhenSendFailed +
                ", consumeThreadMin=" + consumeThreadMin +
                ", consumeThreadMax=" + consumeThreadMax +
                ", client=" + client.toString() +
                '}';
    }

    public static class ClientLog{

        private String logRoot = "/data/server/rocketMq/log";
        private String logLevel = "INFO";
        private int logFileMaxIndex = 10;


        @PostConstruct
        public ClientLog systemProperty(){
            System.setProperty(ClientLogger.CLIENT_LOG_ROOT,logRoot);
            System.setProperty(ClientLogger.CLIENT_LOG_LEVEL,logLevel);
            System.setProperty(ClientLogger.CLIENT_LOG_MAXINDEX,logFileMaxIndex+"");
            return this;
        }

        public String getLogRoot() {
            return logRoot;
        }

        public void setLogRoot(String logRoot) {
            this.logRoot = logRoot;
        }

        public String getLogLevel() {
            return logLevel;
        }

        public void setLogLevel(String logLevel) {
            this.logLevel = logLevel;
        }

        public int getLogFileMaxIndex() {
            return logFileMaxIndex;
        }

        public void setLogFileMaxIndex(int logFileMaxIndex) {
            this.logFileMaxIndex = logFileMaxIndex;
        }

        @Override
        public String toString() {
            return "ClientLog{" +
                    "logRoot='" + logRoot + '\'' +
                    ", logLevel='" + logLevel + '\'' +
                    ", logFileMaxIndex=" + logFileMaxIndex +
                    '}';
        }
    }
}
